package com.wg.controller;

import com.wg.R;
import com.wg.entity.Subtask;
import com.wg.entity.WgInfos;
import com.wg.entity.dto.SubTaskUpdateVo;
import com.wg.entity.dto.SubTaskVo;
import com.wg.entity.dto.WgInfoAddDto;
import com.wg.entity.vo.PageVo;
import com.wg.service.WgInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/wginfo")
public class WgInfoController {

    @Autowired
    WgInfoService wgInfoService;

    /**
     * web端任务相关接口
     */

    @PostMapping
    public R addWG(@RequestBody @Validated WgInfoAddDto wgInfo) {
        return wgInfoService.addWG(wgInfo);
    }

    @PatchMapping("/completeWG")
    public R completeWG(@RequestBody @Validated SubTaskUpdateVo subtask) {
        return wgInfoService.completeWG(subtask);
    }

    /**
     * 获取所有任务
     *
     * @param pageSize 每页大小
     * @param pageNum  当前页数
     * @return wg集合
     */
    @GetMapping("/findAll")
    public R<PageVo<WgInfos>> queryWG(@RequestParam(name = "pagesize") Integer pageSize,
                                      @RequestParam(name = "pagenum") Integer pageNum) {

        return wgInfoService.findAll(pageSize, pageNum);

    }

    @GetMapping("/findOwnedWGById")
    public R<PageVo<WgInfos>> findOwnedWGById(@RequestParam(name = "pagesize") Integer pageSize,
                                              @RequestParam(name = "pagenum") Integer pageNum,
                                              @RequestParam(name = "userId") Integer userId) {
        return wgInfoService.findOwnedWGById(pageSize, pageNum, userId);
    }

    @GetMapping("/findJoinedWGById")
    public R<PageVo<WgInfos>> findJoinedWGById(@RequestParam(name = "pagesize") Integer pageSize,
                                               @RequestParam(name = "pagenum") Integer pageNum,
                                               @RequestParam(name = "userId") Integer userId) {
        return wgInfoService.findJoinedWGById(pageSize, pageNum, userId);
    }

    @GetMapping("/findFocusedWGById")
    public R<PageVo<WgInfos>> findFocusedWGById(@RequestParam(name = "pagesize") Integer pageSize,
                                                @RequestParam(name = "pagenum") Integer pageNum,
                                                @RequestParam(name = "userId") Integer userId) {
        return wgInfoService.findFocusedWGById(pageSize, pageNum, userId);
    }


    @GetMapping("/queryWG")
    public R<PageVo<WgInfos>> queryWG(@RequestParam(name = "pagesize") Integer pageSize,
                                      @RequestParam(name = "pagenum") Integer pageNum,
                                      @RequestParam(name = "startTime", required = false) String startTime,
                                      @RequestParam(name = "endTime", required = false) String endTime,
                                      @RequestParam(name = "creator", required = false) String creator,
                                      @RequestParam(name = "focusUserId", required = false) Long focusUserId,
                                      @RequestParam(name = "focusFlag", required = false) Boolean focusFlag,
                                      @RequestParam(name = "wgName", required = false) String wgName) {
        if (focusFlag == null) {
            focusFlag = false;
        }
        return wgInfoService.queryWG(pageSize, pageNum, startTime, endTime, creator, focusUserId, focusFlag, wgName);

    }

    @GetMapping("/{id}")
    public R<WgInfos> getWgById(@PathVariable Long id) {
        return wgInfoService.getWgById(id);
    }

    /**
     * 根据wgId查找加入的成员
     */
    @GetMapping("/getMembers")
    public R<List<Subtask>> getMembers(@RequestParam(name = "wgId") Integer wgId) {
        return wgInfoService.getMembers(wgId);
    }

    /**
     * 删除wg中的成员，实际删除的是subtask
     */

    @DeleteMapping("/deleteMember")
    public R deleteMember(@RequestBody SubTaskVo task) {
        return wgInfoService.deleteMember(task);
    }


}
